package com.mjk.common.core.health.info;

import com.mjk.common.util.ComputerUtils;
import com.mjk.common.util.TimeUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootVersion;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * info 节点信息添加
 */
@Component
public class AppInfoContributor implements InfoContributor {

    @Value("${spring.application.name:app}")
    private String appName;
    @Value("${server.port:8080}")
    private String port;
    @Override
    public void contribute(Info.Builder builder) {
        Map<String,Object> infoMap = new HashMap<>();
        infoMap.put(HealthConstns.HEALTH_APPINFO_NAME, appName);
        infoMap.put(HealthConstns.HEALTH_APPINFO_ADDR, ComputerUtils.getIpAddress());
        infoMap.put(HealthConstns.HEALTH_APPINFO_PORT, port);
        infoMap.put(HealthConstns.HEALTH_APPINFO_RUNTIME, TimeUtils.formatTime(new Date(), TimeUtils.DATE_FORMAT_DATETIME));
        infoMap.put(HealthConstns.HEALTH_APPINFO_VERSION, SpringBootVersion.getVersion());
        //添加mateinfo
        Map<String, String> metadata = new HashMap<>();
        //服务存储路径
        File currentDirectory = new File("").getAbsoluteFile();
        metadata.put("path", currentDirectory.getParent());
        //系统信息
        metadata.put("os", System.getProperty("os.name"));
        infoMap.put(HealthConstns.HEALTH_APPINFO_MATEDATE, metadata);
        builder.withDetails(infoMap);
    }
}
